package zero.tech.games.gate.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

import zero.tech.core.data.Resp;
import zero.tech.core.data.SimpleUser;
import zero.tech.games.common.Code;
import zero.tech.games.common.RedisConsts;
import zero.tech.games.common.RoomNodeUtil;
import zero.tech.games.common.UserLastRoomUtil;
import zero.tech.games.common.data.RoomNode;
import zero.tech.games.common.data.UserLastRoom;
import zero.tech.games.entity.games.CfServer;
import zero.tech.games.entity.games.Club;
import zero.tech.games.entity.games.ClubGameRule;
import zero.tech.games.entity.games.ClubMember;
import zero.tech.games.gate.hot.CfUtil;
import zero.tech.games.gate.hot.ClubUtil;
import zero.tech.games.gate.service.api.IGateService;
import zero.tech.wsnet.data.CheckPipeiQueue;
import zero.tech.wsnet.data.CheckUserLastRoom;

@RestController
public class RoomController extends BaseController{
	@Autowired
	private UserLastRoomUtil userLastRoomUtil;
	@Autowired
	private RoomNodeUtil roomNodeUtil;
	@Autowired
	private IGateService gateService;
	@Autowired
	private ClubUtil clubUtil;
	@Autowired
	private @Qualifier("restTemplate") RestTemplate restTemplate;
	@Autowired
	private @Qualifier("directTemplate") RestTemplate directTemplate;
	@Autowired
	private CfUtil cfUtil;
	@RequestMapping("/user-last-room")
	public Resp userLastRoom(HttpServletRequest request) {
		SimpleUser simpleUser=super.getLoginUser(request);
		int uid=simpleUser.getUid();
		gateService.initGateData(uid);
		UserLastRoom userLastRoom=userLastRoomUtil.getUserLastRoom(String.valueOf(uid));
		if(userLastRoom!=null) {
			Map<String, String> checkroomParamMap = new HashMap<String, String>();
			checkroomParamMap.put("room_id", userLastRoom.getRoom_id());
			checkroomParamMap.put("club_id",userLastRoom.getClub_id());
			checkroomParamMap.put("uid",String.valueOf(userLastRoom.getUid()));
			CfServer cfServer=cfUtil.getCfServer(userLastRoom.getGame_id());
			//检验房间是否存在
			boolean roomExsist=directTemplate.postForObject("http://"+userLastRoom.getHost()+":"+userLastRoom.getPort()+cfServer.getContext_url()+"/check-lastroom-exsist", checkroomParamMap, Boolean.class);
			if(!roomExsist) {
				//房间已不存
				return Resp.build(Code.OK, "用户最后的房间信息");
			}
			return Resp.build(Code.OK, "用户最后的房间信息",userLastRoom);
		}else {
			return Resp.build(Code.OK, "用户最后的房间信息");
		}
	}
	
	@CheckUserLastRoom
	@CheckPipeiQueue
	@RequestMapping("/create-nomal-room")
	public Resp createNomalRoom(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		SimpleUser simpleUser=super.getLoginUser(request);
		String config_str=(String) map.get("config_str");
		int game_id=Integer.valueOf(map.get("game_id").toString());
		String simpleUserStr=JSONObject.toJSONString(simpleUser);
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("simpleUserStr", simpleUserStr);
		paramMap.put("configStr", config_str);
		paramMap.put("game_id", String.valueOf(game_id));
		CfServer cfServer=cfUtil.getCfServer(game_id);
		return restTemplate.postForObject("http://"+cfServer.getService_id()+cfServer.getContext_url()+"/create-nomal-room", paramMap, Resp.class);
	}
	
	
	
	@CheckUserLastRoom
	@CheckPipeiQueue
	@RequestMapping("/create-club-room")
	public Resp createClubRoom(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		String config_str=(String) map.get("config_str");
		int game_id=Integer.valueOf(map.get("game_id").toString());
		int club_id=Integer.valueOf(map.get("club_id").toString());
		SimpleUser simpleUser=super.getLoginUser(request);
		int uid=simpleUser.getUid();
		ClubMember clubMember=clubUtil.getClubMember(uid,club_id);
		if(clubMember==null) {
			return Resp.build(Code.PARAM_ERROR, "不属于该茶馆");
		}
		Club club=clubUtil.loadHotClub(club_id);
		String simpleUserStr=JSONObject.toJSONString(simpleUser);
		String club_str=JSONObject.toJSONString(club);
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("simpleUserStr", simpleUserStr);
		paramMap.put("configStr", config_str);
		paramMap.put("clubStr", club_str);
		paramMap.put("game_id", String.valueOf(game_id));
		CfServer cfServer=cfUtil.getCfServer(game_id);
		return restTemplate.postForObject("http://"+cfServer.getService_id()+cfServer.getContext_url()+"/create-club-room", paramMap, Resp.class);
	}
	
	@CheckUserLastRoom
	@CheckPipeiQueue
	@RequestMapping("/join-nomal-room")
	public Resp joinNomalRoom(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		SimpleUser simpleUser=super.getLoginUser(request);
		String club_id=RedisConsts.A_DEFAULTCLUB;
		String room_id=(String) map.get("room_id");
		RoomNode roomNode=roomNodeUtil.getRoomNode(club_id,room_id);
		if(roomNode==null) {
			//房间已不存
			return Resp.build(Code.ROOM_NOT_EXSIST, "房间已不存在");
		}
		if(roomNode.getAi()==1) {
			return Resp.build(Code.ROOM_PERSON_FULL, "房间人满了");
		}
		
		String host=roomNode.getHost();
		String port=roomNode.getPort();
		int game_id=roomNode.getGame_id();
		CfServer cfServer=cfUtil.getCfServer(game_id);
		Map<String, String> checkroomParamMap = new HashMap<String, String>();
		checkroomParamMap.put("room_id", room_id);
		checkroomParamMap.put("club_id",club_id);
		//检验房间是否存在
		boolean roomExsist=directTemplate.postForObject("http://"+host+":"+port+cfServer.getContext_url()+"/check-room-exsist", checkroomParamMap, Boolean.class);
		if(!roomExsist) {
			//房间已不存
			return Resp.build(Code.ROOM_NOT_EXSIST, "房间已不存在");
		}
		String simpleUserStr=JSONObject.toJSONString(simpleUser);
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("simpleUserStr", simpleUserStr);
		paramMap.put("room_id", room_id);
		return directTemplate.postForObject("http://"+host+":"+port+cfServer.getContext_url()+"/join-nomal-room", paramMap, Resp.class);
	}
	
	
	@CheckUserLastRoom
	@CheckPipeiQueue
	@RequestMapping("/join-club-room")
	public Resp joinClubRoom(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		SimpleUser simpleUser=super.getLoginUser(request);
		int club_id=Integer.valueOf(map.get("club_id").toString());
		String room_id=(String) map.get("room_id");
		int uid=simpleUser.getUid();
		ClubMember clubMember=clubUtil.getClubMember(uid,club_id);
		if(clubMember==null) {
			return Resp.build(Code.PARAM_ERROR, "不属于该茶馆");
		}
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "茶馆不存在");
		}
		RoomNode roomNode=roomNodeUtil.getRoomNode(String.valueOf(club_id),room_id);
		if(roomNode==null) {
			//房间已不存
			return Resp.build(Code.ROOM_NOT_EXSIST, "房间已不存在");
		}	
		if(roomNode.getAi()==1) {
			return Resp.build(Code.ROOM_PERSON_FULL, "房间人满了");
		}
		String host=roomNode.getHost();
		String port=roomNode.getPort();
		int game_id=roomNode.getGame_id();
		CfServer cfServer=cfUtil.getCfServer(game_id);
		Map<String, String> checkroomParamMap = new HashMap<String, String>();
		checkroomParamMap.put("room_id", room_id);
		checkroomParamMap.put("club_id",String.valueOf(club_id));
		//检验房间是否存在
		boolean roomExsist=directTemplate.postForObject("http://"+host+":"+port+cfServer.getContext_url()+"/check-room-exsist", checkroomParamMap, Boolean.class);
		if(!roomExsist) {
			//房间已不存
			return Resp.build(Code.ROOM_NOT_EXSIST, "房间已不存在");
		}
		String simpleUserStr=JSONObject.toJSONString(simpleUser);
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("simpleUserStr", simpleUserStr);
		paramMap.put("room_id", room_id);
		return directTemplate.postForObject("http://"+host+":"+port+cfServer.getContext_url()+"/join-club-room", paramMap, Resp.class);
	}
	
	
	
	
	@CheckUserLastRoom
	@CheckPipeiQueue
	@RequestMapping("/quick-join-club-room")
	public Resp quickJoinClubRoom(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		SimpleUser simpleUser=super.getLoginUser(request);
		int rule_id=Integer.valueOf(map.get("rule_id").toString());
		ClubGameRule clubGameRule=clubUtil.loadHotClubGameRule(rule_id);
		int club_id=clubGameRule.getClub_id();
		int game_id=clubGameRule.getGame_id();
		int uid=simpleUser.getUid();
		ClubMember clubMember=clubUtil.getClubMember(uid,club_id);
		if(clubMember==null) {
			return Resp.build(Code.PARAM_ERROR, "不属于该茶馆");
		}
		Club club=clubUtil.loadHotClub(club_id);
		String simpleUserStr=JSONObject.toJSONString(simpleUser);
		String config_str=clubGameRule.getConfig_str();
		String club_str=JSONObject.toJSONString(club);
		String pipei_key=clubGameRule.getPipei_key();
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("simpleUserStr", simpleUserStr);
		paramMap.put("configStr", config_str);
		paramMap.put("clubStr", club_str);
		paramMap.put("game_id", String.valueOf(game_id));
		paramMap.put("pipei_key",pipei_key);
		CfServer cfServer=cfUtil.getCfServer(game_id);
		return restTemplate.postForObject("http://"+cfServer.getService_id()+cfServer.getContext_url()+"/quick-join-club-room", paramMap, Resp.class);
	}
}
